問題描述
大寫替代字母 (Alternate letters in upper case)
因為下面的代碼應該將給定的字符串轉換為可選的大寫或小寫。字符串 S(僅限字母)作為輸入傳遞。打印輸出應包含每個單詞的奇數位置的大寫字母和偶數位置的小寫字母。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str[100];
int i;
scanf("%s",str);
for(i=0;str[i]!='\0';i++){
if((i%2)==1)
str[i]=tolower(str[i]);
else
str[i]=toupper(str[i]);
}
printf("%s",str);
return 0;
}
輸入將是 tREE GiVES us fruiTS
和預期的輸出應該是 TrEe GiVeS Us FrUiTs
但我得到的只是第一個字符串 TrEe 我應該怎麼做才能得到完整的字符串
參考解法
方法 1:
scanf("%s, str)
reads in a string until the first whitespace character. So when you type "tree gives us fruits" it reads in "tree" and then see's the whitespace and stops.
Try using fgets(str, 100, stdin)
instead
https://www.cplusplus.com/reference/cstdio/fgets/